const axios = require("axios");

const RateService = require("../../services/web/RateService")
// const alipaySdk = require('../alipay');

const AlipaySdk = require('alipay-sdk').default
const AlipayFormData = require('alipay-sdk/lib/form').default


const RateController = {
    
    //引入支付宝沙箱配置


    //支付接口
    rate: async (req, res, next) => {

        console.log("数据：", JSON.parse(req.params.data).orderId)
        const alipaySdk = new AlipaySdk({
            appId: '9021000128667952',
            // gateway: 'https://openapi.alipaydev.com/gateway.do',
            gateway: 'https://openapi-sandbox.dl.alipaydev.com/gateway.do',
            signType: 'RSA2', // 注意这里默认是RSA2, 但是我自己只能用RSA, 所以是RSA, 正常不要配置
            privateKey: 'MIIEowIBAAKCAQEAqKR6kXMi7BufMT4lnc7Ox+S+H/YY0p8AxD9C1rcq1k9AK7VFF8ULLc1ggHkQSeOdxLi7X2qlx4mSGGjAbv/i7swNfZfQMnXkYhPiJDc3EQM10OSS4zoR6E6NFUzrQjaGkHA0ZBC1rme7VWayuDHX/Z93f7h84w9SiHjcM5Y8qBnMCPy1Di3TpJMPTWqa2WzOdHO/vF18waJ+y4C2trLzA6acEXfbYHSOZbDceJBXRqM6qyt0as/Pf4RIWNZvVyPOl8ZWhMK7msT5EZYgeMC+VxD5dUmXD2reVYsaZChUPezYyDftRkW0fUHj0jmlpZ5l6NLNJKovqdsZVCla9+bR4wIDAQABAoIBAA23y2yXdAjGum1UA9VRZEqoVbrd1ukTHAnap4I1xhtGuQ6VITisw5sHMMlij6ujA0tkR4GceD3B+AtaQgUFdZ09Jf/IYJwqMNi1i1is0SLmIvOKFFS9fYVrFljcN+rb6phHOEvHfC/iqj9pKjKHg21TEnJmPGpv7aPCSQ89MlPuG18jBgQJdd6IDX5n/G5xvD3DmbAb5a8qh287/l0MZO689QLIi+HecFcj8fFJW20WVNLTPTnQNwp3qFVJXaJPA0QBuzNzX8QwKttU8cDPcPcVip68cgEIsw+ezT6FH6mF0sIr0CnHdZ3hG9khYmXVz5h48VsDR5qgw8re9n+NZkkCgYEA4CiGqo7i/Y2ZvbjoC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WlBaG87n9pcqcIKVlta9BqTJlBhFm7V7L4QySxkCYvoMyx9GG/GzU5D+ViCU/JJmKx8Tsm/qwuPRAwrY1zlGQKZnzDYsITpru/2cAbXM2Jpa9of3jSHqeP5F65vCBZ4MBeYO3EJMHaiE8nQfO2v+QmVWgkzfGOH1K4blIYWUOJOmRUIRbbhMlX1H79Rf5X8pswIDAQAB'
        })

        async function pay() {
            const formData = new AlipayFormData()
            // 调用 setMethod 并传入 get，会返回可以跳转到支付页面的 url
            formData.setMethod('get')
            // 配置回调接口
            formData.addField('returnUrl', 'http://localhost:8081/#/payment')

            // 设置参数
            formData.addField('bizContent', {
                outTradeNo: JSON.parse(req.params.data).orderId,
                productCode: 'FAST_INSTANT_TRADE_PAY',
                totalAmount: JSON.parse(req.params.data).price,
                subject: '物业费缴费',
                body: `${JSON.parse(req.params.data).name}业主费用缴纳`,
            });
            // 请求接口
            const result = await alipaySdk.exec(
                'alipay.trade.page.pay',
                //   "alipay.trade.query",
                {},
                { formData: formData },
            );
            const result2 = await alipaySdk.exec(
                // 'alipay.trade.page.pay',
                "alipay.trade.query",
                {},
                { formData: formData },
            );
            // console.log("返回值1为:",result)
            // console.log("返回值21为:",result2)


            res.send({
                ActionType: "ok",
                data: result,
                data2: result2,
            })
        }

        pay()


    },
    changeispay: async (req, res) => {
        const result = await RateService.changeispay({ _id: req.params.id })
        res.send({
            ActionType: "ok",
        })
    },
    webgetList: async (req, res) => {
        // console.log("controller:",req.params.data)
        const result = await RateService.webgetList(req.params.data)
        res.send({
            ActionType: "ok",
            data: result
        })
    },
    webgetallList: async (req, res) => {
        // console.log("controller:",req.params.data)
        const result = await RateService.webgetallList(req.params.data)
        res.send({
            ActionType: "ok",
            data: result
        })
    },


}

module.exports = RateController